package com.zhku.onlineshop.Dao;

import com.zhku.onlineshop.Pojo.Product;
import com.zhku.onlineshop.dto.ProductRating;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductDao {
    @Select("SELECT * from products")
    List<Product> GetProductsList();

    @Insert("INSERT INTO products(shop_name,shop_description,shop_price,shop_img,shop_id) values (#{shop_name},#{shop_description},#{shop_price},#{shop_img},#{shop_id})")
    boolean AddProduct(Product product);

    @Select("SELECT * from products WHERE shop_id = #{id}")
    List<Product> GetProductById(@Param("id") Integer id);

    @Delete("DELETE FROM products WHERE id = #{id}")
    boolean DelProductById(@Param("id") Integer id);

    @Update("UPDATE products SET shop_name = #{shop_name},  shop_description=#{shop_description},shop_price=#{shop_price}  WHERE id=#{id}")
    boolean UpdProduct(Product product);
    @Select("SELECT * FROM product_ratings WHERE product_id=#{id}")
    List<ProductRating> GetRattingById(@Param("id") Integer productId);

    @Insert("INSERT INTO product_ratings (product_id, username, rating, comment) " +
            "VALUES (#{product_id}, #{username}, #{rating}, #{comment})")
    boolean AddRatting(ProductRating productRating);


    @Select("SELECT * FROM products WHERE id IN (${ids})")
    List<Product> selectProductsByIds(@Param("ids") String ids);


    // 查询 quantity 相加数量前十的 product_id
    @Select("SELECT product_id FROM order_items GROUP BY product_id ORDER BY SUM(quantity) DESC LIMIT 10")
    List<Integer> selectTopTenProductIds();
}
